import { createI18n } from "vue-i18n";
import pinia from '@/store';
import { storeToRefs } from 'pinia'
import { useThemeConfig } from '@/store/modules/themeConfig'

//引入框架语言配置
import zhcnLocale from 'element-plus/lib/locale/lang/zh-cn'
import enLocale from 'element-plus/lib/locale/lang/en'

//引入自定义语言配置
import nextZhcn from '@/language/lang/zh'
import nextEn from '@/language/lang/en'

//引入页面语言配置
import pagesLoginZhcn from '@/language/pages/login/zh'
import pagesLoginEn from '@/language/pages/login/en'
import pagesFormI18nZhcn from '@/language/pages/formI18n/zh'
import pagesFormI18nEn from '@/language/pages/formI18n/en'
import { local } from "@/utils";
import { GlobalI18n } from "@/global/const";

const messages = {
  [zhcnLocale.name]: {
    ...zhcnLocale,
    message: {
      ...nextZhcn,
      ...pagesLoginZhcn,
      ...pagesFormI18nZhcn,
    },
  },
  [enLocale.name]: {
    ...enLocale,
    message: {
      ...nextEn,
      ...pagesLoginEn,
      ...pagesFormI18nEn,
    },
  },
}

// 读取 pinia 默认语言
const stores = useThemeConfig(pinia)
const { themeConfig } = storeToRefs(stores)

// 导出语言国际化
export const i18n = createI18n({
  legacy: false,
  silentTranslationWarn: true,
  missingWarn: false,
  silentFallbackWarn: true,
  fallbackWarn: false,
  locale: local.get(GlobalI18n) || window.navigator.language, // 采用全局参数配置初始化语言 项目中有`zh-cn`、`en`两种
  fallbackLocale: zhcnLocale.name,
  messages,
})

